﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiMinCRMPlugin
{

    [Description("采购入库单暂存时更新数据")]
    [HotUpdate]
    public class InStockBeforeService: AbstractOperationServicePlugIn
    {
        public override void OnPreparePropertys(PreparePropertysEventArgs e)
        {
            base.OnPreparePropertys(e);
            e.FieldKeys.Add("FID");
            e.FieldKeys.Add("FSRCBILLNO");
            e.FieldKeys.Add("FPOORDERNO");
            e.FieldKeys.Add("FDATASOURCE");
        }
        public override void AfterExecuteOperationTransaction(AfterExecuteOperationTransaction e)
        {
            base.AfterExecuteOperationTransaction(e);
            foreach (DynamicObject entity in e.DataEntitys)
            {
                string FID = Convert.ToString(entity["ID"]);
                if (Convert.ToString(entity["FDATASOURCE"]) == "智慧物流")
                {
                    string sql = string.Format(@"/*dialect*/UPDATE B SET B.FSRCROWID=D.FENTRYID ,B.FRECEIVEOWNERTYPEID=D.FOWNERTYPEID,B.FRECEIVEOWNERID=D.FOWNERID,
                                        B.FRECEIVELOT=D.FLOT,B.FRECEIVESTOCKID=D.FSTOCKID,B.FRECEIVESTOCKLOCID=D.FSTOCKLOCID,B.FRECEIVEAUXPROPID=D.FAUXPROPID,B.FRECEIVEMTONO=D.FMTONO
                                        FROM T_STK_INSTOCK A
                                        LEFT JOIN T_STK_INSTOCKENTRY B ON A.FID=B.FID
                                        LEFT JOIN T_PUR_RECEIVE C ON B.FSRCBILLNO = C.FBILLNO
                                        LEFT JOIN T_PUR_RECEIVEENTRY D ON C.FID=D.FID
                                        INNER JOIN T_STK_INSTOCKENTRY_LK E ON B.FENTRYID=E.FENTRYID AND E.FSBILLID=C.FID AND E.FSID=D.FENTRYID AND E.FRULEID='PUR_ReceiveBill-STK_InStock'
                                        WHERE A.FDATASOURCE='智慧物流' AND A.FID='" + FID+"'");
                    DBUtils.Execute(Context, sql);

                }

            }
        }
    }
}
